<!-- 域账号登录 -->
<template>
    <div></div>
</template>

<script>
import { authApi, refreshToken } from "@/api/user";
import { authLoginUrl } from '@/config/env';
export default {
    data() {
        return {

        };
    },
    mounted() {
        this.getNewToken();
    },
    computed: {

    },
    methods: {
        getNewToken() {
            // var _this = this;
            var tenantId = JSON.parse(window.localStorage.getItem('justech-tenantId'));
            var refreshTokenStr = JSON.parse(window.localStorage.getItem('justech-refreshToken'));
            if (refreshTokenStr != null) {
                refreshToken(refreshTokenStr.content, tenantId.content).then((res) => {
                    if (res.status != 200) {
                        this.auth()
                    } else {
                        this.$store.dispatch('authLogin', res.data).then(() => {
                            // console.log('登录成功，进入首页!');
                            // 存储最新token
                            // console.log( res.data,'token信息')
                            this.$store.dispatch('NewRefreshToken', res.data)
                            window.history.pushState('', '', `${authLoginUrl}/#/portal`);
                            this.$router.push('/portal/index') // 默认跳转首页
                            // console.log(res,'刷新token');
                            // const token = JSON.parse(window.localStorage.getItem('justech-token'));
                            // const refreshToken = JSON.parse(window.localStorage.getItem('justech-refreshToken'));
                            let patams = {
                                token: res.data.content.access_token,
                                refreshToken: res.data.content.refresh_token
                            }
                            // WsConnection.createWS(patams); 
                        });
                    }
                }).catch(() => {
                    this.auth()
                })
            } else {
                this.auth()
            }
        },
        auth() {
            if (this.getQueryVariable('token')) {
                // 调接口
                const params = {
                    adKey: this.getQueryVariable('token'),
                    grant_type: 'AD',
                    scope: 'all',
                    type: 'account'
                }
                authApi(params).then((res) => {
                    // alert('成功！')
                    // console.log(res.data,'res.datares.data');
                    this.$store.dispatch('authLogin', res.data).then(() => {
                        //   console.log('登录成功，进入首页!');
                        //   sessionStorage.setItem('logOut', true)
                        this.$store.dispatch('NewRefreshToken', res.data)
                        window.history.pushState('', '', `${authLoginUrl}/#/portal`);
                        this.$router.push('/portal/index') // 默认跳转首页
                        // const token = JSON.parse(window.localStorage.getItem('justech-token'));
                        // const refreshToken = JSON.parse(window.localStorage.getItem('justech-refreshToken'));
                        let patams = {
                            token: res.data.content.access_token,
                            refreshToken: res.data.content.refresh_token
                        }
                        // WsConnection.createWS(patams);
                    });
                }).catch(function (error) {
                    // alert('失败！')
                    console.log(error, '登录失败跳转登录页！');
                    sessionStorage.setItem('logOut', true)
                    window.location.href = `${authLoginUrl}/#/login`;
                });
                // window.history.pushState('', '', 'http://10.65.44.28:10099/#/login');
                // this.$router.replace({
                //     path: '/login'
                // });
            } else {
                // alert('sso！')
                // window.location.href = `http://sso.justech.com/v3/?back_url=${authLoginUrl}&AppID=MES`;
                window.location.href = `http://10.66.52.102/v3/?back_url=${authLoginUrl}&AppID=MES`;
            }
        },
        // 获取url 参数
        getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return false;
        }
    },

};
</script>

<style>
</style>
